//
//  UIImageUtil.m
//  ImageMerge
//
//  Created by alex tao on 2/16/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "UIImageUtil.h"

#define BYTES_PER_PIXEL             4
#define BYTES_PER_COMPONENT         8

void DataProviderReleaseDataCallback(void *info, const void *data, size_t size)
{
    free((void*)data);
}

@implementation UIImageUtil

+ (uint32_t*)getimageData:(UIImage *) image
{
    CGImageRef imageref = [image CGImage];
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    //创建临时上下文
    int width = CGImageGetWidth(imageref);
    int height = CGImageGetHeight(imageref);
    int bytesPerRow = BYTES_PER_PIXEL * width;
    uint32_t* imagedata = (uint32_t*)malloc(bytesPerRow * height);
    CGContextRef cgcnt = CGBitmapContextCreate(imagedata,
                                               width,
                                               height,
                                               BYTES_PER_COMPONENT,
                                               bytesPerRow,
                                               colorspace,
                                               kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast
                                               );
    //将图像写入一个矩形
    CGContextDrawImage(cgcnt,CGRectMake(0,0,width,height),imageref);
    
    //释放资源
    CGColorSpaceRelease(colorspace);
    CGContextRelease(cgcnt);    
    
    return imagedata;
}

+ (UIImage*)uiimageFromImageData:(uint32_t*)data size:(CGSize)size scale:(CGFloat)scale orintation:(UIImageOrientation)orintation;
{
    int bytesPerRow = BYTES_PER_PIXEL * size.width;
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL,
                                                              data, 
                                                              bytesPerRow * size.height, 
                                                              DataProviderReleaseDataCallback
                                                              );
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    CGImageRef imageref = CGImageCreate(size.width,
                                        size.height,
                                        BYTES_PER_COMPONENT, 
                                        32, 
                                        bytesPerRow, 
                                        colorspace, 
                                        kCGImageAlphaLast | kCGBitmapByteOrder32Little, 
                                        provider, 
                                        NULL, 
                                        FALSE, 
                                        kCGRenderingIntentDefault
                                        );
    CGDataProviderRelease(provider);
    
    
    UIImage * drawimage = [UIImage imageWithCGImage:imageref scale:scale orientation:orintation];
    
    CGColorSpaceRelease(colorspace);
    CGImageRelease(imageref);
    
    return drawimage;
}

@end
